﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NWN.GDY.Service.Entity;
using Sdo.Payment.Common;
using NWN.GDY.Service.Common;

namespace NWN.GDY.Service.Core.HandcardCreator
{
    public class DoubleCreator : AbstractHDCreator
    {
        AbstractHDCreator hdcreator;
        string description = "对子";  
        public override string GetDescription()
        {
            if (hdcreator != null)
                return hdcreator.GetDescription() + "_" + description;
            return description;
        }

        public DoubleCreator(AbstractHDCreator creator)
        {
            this.hdcreator = creator;
        }
        public DoubleCreator()
        {
        }

        public override void CreateHandCard(List<Card> cards, List<Card> AllCards, List<HandCard> handcards)
        {
            if (hdcreator != null)
            {
                hdcreator.CreateHandCard(cards, AllCards, handcards);
            }
            if (cards.Count != 2)
                return;
            if (cards[0].CardType > 4 && cards[1].CardType > 4)
                return;
            //顺序排列，只有第二张可能是王
            if (cards[1].CardType > 4)
            {
                Card createcard = (from c in AllCards where c.CardNum == cards[0].CardNum && c.CardType == (int)CardType.jokeraltern select c).FirstOrDefault();
                if (createcard == null)
                    throw new Exception("转换王牌异常");
                List<Card> tempcards = new List<Card>();
                tempcards.Add(cards[0]);
                tempcards.Add(createcard);
                //Log.Lw("带王一对" + cards[0].SimpleName);
                handcards.Add(new HandCard() { Cards = tempcards, Type = HandCardType.Double });
            }
            else if (cards[0].CardNum != cards[1].CardNum)
                return;
            else
            {
                //Log.Lw("一对" + cards[1].SimpleName);
                handcards.Add(new HandCard() { Cards = cards, Type = HandCardType.Double });
            }
        }

        public override int CompareHandCard(HandCard lasthc, HandCard currenthc)
        {
            if (hdcreator != null)
            {
                if (hdcreator.CompareHandCard(lasthc, currenthc) == GDYConsts.ReturnDefine.SUCCESS)
                    return GDYConsts.ReturnDefine.SUCCESS;
            }
            if (lasthc.Type != HandCardType.Double || currenthc.Type != HandCardType.Double)
                return GDYConsts.ReturnDefine.HANDCARDTYPEERROR;
            if (lasthc.Cards[0].CardNum == 2)
                return GDYConsts.ReturnDefine.HANDCARDCOMPAREWRONG;
            if (currenthc.Cards[0].CardNum == 2)
                return GDYConsts.ReturnDefine.SUCCESS;
            if (lasthc.Cards[0].CardNum + 1 == currenthc.Cards[0].CardNum)
                return GDYConsts.ReturnDefine.SUCCESS;
            return GDYConsts.ReturnDefine.HANDCARDCOMPAREWRONG;
        }

    }
}
